function getHttp(a, data, callback) {
    $.ajax({
        url: sdk.baseIP + a,
        data: data,
        dataType: 'jsonp',
        success: function(d, r, x) {
            callback(d, r, x);
        }
    });
}

function toBack() {
    window.history.back();
}


//监听登录选择
var user = {
    '4': {
        name: '企业用户',
        register: true,
        path: 'zxw/company/xq/release_demand',
        yjfk: 'zxw/company/ts/complaint_admin'
    },
    '5': {
        name: '个人用户',
        register: true
    },
    '1': {
        name: '银行用户',
        register: false,
        path: 'zxw/jrjg/bankDbsx/wdxq',
        yjfk: 'zxw/jrjg/bankDbsx/tsgl',
        cpdel: 'mh/app/product/proInfo',
        clName: '#bankJgDel'
    },
    '3': {
        name: '政府部门',
        path: 'zxw/gov/app/rzxq_rzytfx?tname=rzytfx',
        yjfk: 'zxw/gov/app/tscl?tname=tscl',
        register: false
    },
    '6': {
        name: '保险用户',
        register: false,
        path: 'zxw/bxjg/wdsp',
        cpdel: 'mh/app/v2/pro/getBxjgProductInfoOne',
        clName: '#bxJgDel'
    },
    '8': {
        name: '证券用户',
        register: false,
        path: 'zxw/zqjg/wdsp',
        cpdel: 'mh/app/v2/pro/getZqProductInfoViewOne',
        clName: '#zqJgDel'
    },
    '2': {
        name: '担保公司',
        register: false,
        path: 'zxw/jrjg/dbgsDbsx/auditList',
        cpdel: 'mh/app/v2/pro/getDbjgProductInfoOne',
        clName: '#dbJgDel'
    },
    '9': {
        name: '投资机构',
        register: false
    },
    '7': {
        name: '金融专员',
        path: 'zxw/jrzy/index/finance_jrzy',
        register: false
    }
};
layui.use(['form', 'layer', 'element', 'upload'], function() {
    var form = layui.form;
    var layer = layui.layer;
    var element = layui.element;
    // 监听底部下拉选择
    form.on('select(urlTo)', function(data) {
        console.log(data);
        if (data.value) {
            window.open(data.value)
        }
    }); 
    // 登录渲染角色类型
    loginMain = function(typeId) {
        // 确定登录角色
        var roleType = '';
        // 登录弹窗w h
        var w = 320 + 'px',
            h = 250 + 'px';
        if (typeId == 'qy') {
            // 单选登录方式
            roleType = "1";
            $('#bklwxo').text('企业登录');
            $('#zwrkoe').show();
            $('#xhpoee').show();
            $('#changeWay').show();
            w = 330 + 'px';
            h = 320 + 'px'
            // 跳转注册
            $('#getkdh').click(function() {
                layer.open({
                    type: 1,
                    title: false,
                    shadeClose: true,
                    area: ['400px', '300px'],
                    content: $('#codeHref'),
                    success: function(layero, index) {},
                    end: function() {
                        form.val("formVcyk", {
                            account: '',
                            password: '',
                            validateCode: ''
                        });
                    }
                });
            });
            // 跳转找回密码
            $('#xhpoee').click(function() {
                forgetPassword();
            });

        } else if (typeId == 'jg') {
            roleType = "2";
            $('#bklwxo').text('机构登录');
            $('#zwrkoe').hide();
            $('#changeWay').hide();
        } else if (typeId == 'zf') {
            roleType = "3";
            $('#bklwxo').text('政府登录');
            $('#zwrkoe').hide();
            $('#changeWay').hide();
        }
        layer.open({
            type: 1,
            title: false,
            shadeClose: true,
            area: [w, h],
            content: $('#layerVcyk'),
            success: function(layero, index) {
                changeValidateImg();
                $('#imgYzm').click(function() {
                    changeValidateImg()
                });
                form.on('submit(formGohk)', function(d) {
                    var rjzpIndex = layer.load(2);
                    var data = d.field;
                    var acc = data.account;
                    var psw = data.password;
                    data.account = sdk.setBase64(acc);
                    data.password = sdk.setBase64(psw);
                    // 已选择的用户角色类型
                    data.typeId = roleType;
                    setTimeout(function() {
                        layer.close(rjzpIndex);
                    }, 5000);
                    $.ajax({
                        type: 'post',
                        data: data,
                        url: sdk.companyIp +
                            'zxw/company/login/loginCheck',
                        success: function(d) {
							console.log('d111',d)
			
                            layer.close(rjzpIndex);
                            consoleMain();
                            changeValidateImg();
                            if (d.code == '00') {
                                layer.msg(d.msg, {
                                    icon: 6,
                                    closeBtn: 1,
                                    time: 3000
                                });
								//授权协议弹框
								 // $.ajax({
									// type: 'get',
									// url: sdk.companyIp + 'zxw/company/sjrfdConfirm/confirmStatus', //授权协议弹框
									// success: function(agreementData) {
									// 	  console.log('agreementData',agreementData);
									// 	  debugger
									// 	  if (agreementData.data && agreementData.data.needConfirm) {
									// 	  	layer.close(index);
									// 	  	agreement(data);
									// 	  }else{
									// 	  	if (user[d.typeId]) {
									// 	  		console.log('d22222233',d)
									// 	  	    if (d.typeId == '3') {
									// 	  	        // 政府角色假登录
									// 	  	        location.href = 'index_gov.html'
									// 	  	    } else {
									// 	  	        location.href = sdk.baseIP + user[d.typeId].path;
									// 	  	    }
									// 	  	}
									// 	  }
									// },
									// error: function(err) {
									//     console.log('agreementData-fail',err);
									// }
								 // })
								
								if (user[d.typeId]) {
									console.log('d22222233',d)
								    if (d.typeId == '3') {
								        // 政府角色假登录
								        location.href = 'index_gov.html'
								    } else {
								        location.href = sdk.baseIP + user[d.typeId].path;
								    }
								}
                            } else {
                                layer.msg(d.msg, {
                                    icon: 2,
                                    closeBtn: 1,
                                    time: 3000
                                });
                            }
                        },
                        error: function(err) {
                            layer.msg('系统错误,请联系管理员!', {
                                icon: 5
                            });
                        }
                    });
                });
            },
            end: function() {
                // form.val("formVcyk", {
                //     account: '',
                //     password: '',
                //     validateCode: ''
                // });
                // window.location.reload();
            }
        });
    };

    // 渲染登录页面
    loginSvkc = function(typeId) {
        if ($('#layerVcyk').length == 0) {
            $.ajax({
                url: 'login2.html',
                dataType: 'html',
                success: function(html) {
                    $('body').append(html);
                    loginMain(typeId);
                }
            });
        } else {
            loginMain(typeId)
        }
    };

    // 改变验证码
    function changeValidateImg() {
        $('#imgYzm').attr('src', '').attr('src', sdk.companyIp +
            'zxw/company/login/getRandomValidateCode?r=' + Math.random());
        form.val("formVcyk", {
            validateCode: ''
        });
    };

    // 角色身份选择
    element.on('nav(loginUser)', function(elem) {
        var code = $(elem.context).attr('code');
        if (code) {
            loginSvkc(code);
        }
    });

    form.verify({
        pass: [/^(?=.*\d)(?=.*?[a-z])(?=.*?[A-Z])(?=.*[~!@#$%^&,*])[\da-zA-Z~!@#$%^&,*]{8,20}$/,
            '密码必须包含大小写字母和数字和特殊字符，长度在8-20之间'
        ]
    });
    // 格式化输入文本
    function filterHTMLTag(msg) {
        var msg = msg.replace(/<[^>]+>|&[^>]+;/g, ''); //去除HTML Tag
        msg = msg.replace(/[|]*\n/, '') //去除行尾空格
        msg = msg.replace(/&npsp;/ig, ''); //去掉npsp
        return msg;
    }
    $('input,textarea').on('input', function() {
        $(this).val(filterHTMLTag($(this).val()))
    });
    // 根据地址参数触发修改密码
    if (sdk.getURLVar('type') === 'mpwd') {
        changePassword();
        history.pushState('', '', 'index.html');
    }

    // 修改密码
    function changePassword() {
        $.ajax({
            url: 'modify_pwd.html',
            dataType: 'html',
            success: function(d) {
                $('body').append(d);
                layer.open({
                    type: 1,
                    title: false,
                    shadeClose: true,
                    content: $('#nhnvve'),
                    success: function(layero, index) {
                        form.on('submit(formTwlh)', function(d) {
                            var data = d.field;
                            if (data.oldPassword === data.newPassword) {
                                layer.msg('新密码和旧密码不能相同', {
                                    icon: 5
                                });
                                return false;
                            }
                            if (data.newPassword !== data.newPasswordRepeat) {
                                layer.msg('两次输入的密码不一致', {
                                    icon: 5
                                });
                                return false;
                            }
                            data.oldPassword = sdk.setBase64(data.oldPassword);
                            data.newPassword = sdk.setBase64(data.newPassword);
                            $.ajax({
                                type: 'post',
                                data: data,
                                url: sdk.baseIP +
                                    'zxw/company/core/changePassword',
                                success: function(d) {
                                    if (d.code === '00') {
                                        layer.close(index);
                                        layer.msg('修改成功', {
                                            icon: 6,
                                            closeBtn: 1,
                                            time: 3000
                                        });
                                        location.href = sdk.baseIP +
                                            'zxw/company/login/loginout'
                                    } else {
                                        layer.msg(d.msg, {
                                            icon: 5,
                                            closeBtn: 1,
                                            time: 3000
                                        });
                                    }
                                },
                                error: function(err) {
                                    layer.msg('系统错误,请联系管理员!', {
                                        icon: 5
                                    });
                                }
                            })
                        })
                    },
                    end: function() {
                        form.val('formPyzj', {
                            oldPassword: '',
                            newPassword: '',
                            newPasswordRepeat: ''
                        })
                    }
                })
            }
        })
    };

    // 忘记密码
    function forgetPassword() {
        $.ajax({
            url: 'forget_password.html',
            dataType: 'html',
            success: function(d) {
                $('body').append(d);
                layer.open({
                    type: 1,
                    title: false,
                    shadeClose: true,
                    area: ['440px', '450px'],
                    content: $('#nhnvve'),
                    success: function(layero, index) {
                        // 发送验证码
                        $('#sendMsgbtn').click(function() {
                            var phone = $('#phone').val();
                            if (!phone) {
                                layer.msg('请输入手机号!', {
                                    icon: 5
                                });
                                return;
                            } else {
                                var t = 60;
                                $('#sendMsgbtn').attr('disabled', true);
                                var st = setInterval(function() {
                                    $('#sendMsgbtn').text(t);
                                    t--;
                                    if (t == 0) {
                                        clearInterval(st);
                                        $('#sendMsgbtn').text('获取验证码');
                                        $('#sendMsgbtn').attr(
                                            'disabled', false);
                                    }
                                }, 1000);
                            }
                            $.ajax({
                                type: 'get',
                                url: sdk.companyIp +
                                    'zxw/company/springLoanLogin/sendCode?phone=' +
                                    $('#phone').val(),
                                success: function(result) {
                                    if (result.flag) {
                                        layer.msg(result.msg, {
                                            icon: 6,
                                            closeBtn: 1,
                                            time: 3000
                                        });

                                    } else {
                                        layer.msg('频繁操作,请稍候再试!', {
                                            icon: 5
                                        });
                                    }
                                },
                                error: function(err) {
                                    layer.msg('系统错误,请联系管理员!', {
                                        icon: 5
                                    });
                                }
                            });

                        });

                        // 提交忘记密码
                        form.on('submit(formTwlh_forget)', function(d) {
                            var data = d.field;
                            if (data.newPassword !== data.newPasswordRepeat) {
                                layer.msg('两次输入的密码不一致', {
                                    icon: 5
                                });
                                return false;
                            }
                            var pubKey = null;
                            $.ajax({
                                type: 'get',
                                async: false,
                                url: sdk.companyIp +
                                    'zxw/company/springLoanLogin/getKey',
                                success: function(result) {
                                    if (result.flag) {
                                        pubKey = result.data;
                                    } else {
                                        layer.msg(result.msg, {
                                            icon: 5
                                        });
                                    }
                                },
                                error: function(err) {
                                    layer.msg('系统错误,请联系管理员!', {
                                        icon: 5
                                    });
                                }
                            });

                            data.newPassword = sdk.setEncrypt(pubKey, data
                                .newPassword);
                            $.ajax({
                                type: 'post',
                                data: data,
                                url: sdk.baseIP +
                                    'zxw/company/h5Login/forgetPassword',
                                success: function(d) {
                                    if (d.flag) {
                                        layer.close(index);
                                        layer.msg('密码重置成功!', {
                                            icon: 6,
                                            closeBtn: 1,
                                            time: 3000
                                        });
                                    } else {
                                        layer.msg(d.msg, {
                                            icon: 5,
                                            closeBtn: 1,
                                            time: 3000
                                        });
                                    }
                                },
                                error: function(err) {
                                    layer.msg('系统错误,请联系管理员!', {
                                        icon: 5
                                    });
                                }
                            })
                        })
                    },
                    end: function() {
                        form.val('formPyzj', {
                            newPassword: '',
                            newPasswordRepeat: '',
                            code: '',
                            phone: '',
                            tyshxydm: ''
                        })
                    }
                })
            }
        })
    };


    // 跳转到工作台
    $('.bankName').click(function() {
        var typeId = $(this).attr('data-val');
        if (user[typeId]) {
            if (typeId == '3') {
                return
            } else {
                location.href = sdk.baseIP + user[typeId].path;
            }
        }
    });

    // 签退
    $('.logoutBtn').click(function() {
        var layer = layui.layer;
        layer.confirm('你确定要退出吗?', {
            icon: 0,
            title: '提示'
        }, function(index) {
            layer.close(index);
            sessionStorage.setItem('baohan_info', '');
            location.href = sdk.baseIP + 'zxw/company/login/loginout';
        });
    });

    // 个人信息
    function consoleMain() {
        $.ajax({
            type: 'post',
            url: sdk.companyIp + 'zxw/company/login/getLoginFlag',
            dataType: 'json',
            success: function(userinfo) {
                if (userinfo.loginFlag == 'true') {
                    $('#isLogin').removeClass('noShow');
                    $('#noLogin').addClass('noShow');
                    $('.bankName').text(userinfo.userName);
                    $('.bankName').attr('data-val', userinfo.userType);
					if(userinfo.userType =='15202'){
						$('#fdjc').show()
					}
                } else {
                    $('#noLogin').removeClass('noShow');
                    $('#isLogin').addClass('noShow');
                }
            },
            error: function(err) {
                layer.msg('系统错误,请联系管理员!', {
                    icon: 5
                });
                $('#noLogin').removeClass('noShow');
                $('#isLogin').addClass('noShow');
            }
        });
    }
    consoleMain();
	
	// 协议授权
	function agreement(d) {
		$.ajax({
		    url: 'agreement.html',
		    dataType: 'html',
		    success: function(data) {
				//console.log('agreement',data)
		        $('body').append(data);
				layer.open({
				    type: 1,
				    title: '服务与授权协议',
				    shadeClose: false,
					closeBtn:0,
				    area: ['440px', '450px'],
					skin: 'agreement-class',
				    content: $('#agreement'),
					success: function(){
						$('#agreement_sumbit').click(function() {
							$.ajax({
							    type: 'get',
							    url: sdk.companyIp + 'zxw/company/sjrfdConfirm/confirm', //q确定授权
								success: function(result) {
									// debugger
									// console.log('result',result)
									if (user[d.typeId]) {
									    if (d.typeId == '3') {
									        // 政府角色假登录
									        location.href = 'index_gov.html'
									    } else {
									        location.href = sdk.baseIP + user[d.typeId].path;
									    }
									}
								}
							})	
						});
						
						$('#agreement_checkbox').click(function(checkData) {
							//console.log('checkData',checkData)
							if(checkData.toElement.checked){
								$('#agreement_sumbit').removeClass('layui-btn-disabled')
							}else{
								$('#agreement_sumbit').addClass('layui-btn-disabled')
							}
						})
					}
				})	
			}
		})	
	}
})
